package com.windfone.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.windfone.model.Fitting;
import com.windfone.model.Order;
import com.windfone.model.PackageCompose;
import com.windfone.model.Product;
import com.windfone.service.CommonService;
import com.windfone.service.FittingService;
import com.windfone.service.ProductService;

public class ShoppingAction extends ActionSupport {

	/**	serialVersionUID */
	private static final long serialVersionUID = 1L;
	private String price;
	private String priceAndColor;
	private int productId=-1;
	private int goodstype;
	private long productHiddenSession;
	private List<Order> orderList ;
	private ProductService productService;
	private FittingService fittingService;
	private long seqTime;
	private int orderNumber;
	private long newProductHiddenSession;
	public long getNewProductHiddenSession() {
		return newProductHiddenSession;
	}
	public void setNewProductHiddenSession(long newProductHiddenSession) {
		this.newProductHiddenSession = newProductHiddenSession;
	}
	public int getOrderNumber() {
		return orderNumber;
	}
	public void setOrderNumber(int orderNumber) {
		this.orderNumber = orderNumber;
	}
	public List<Order> getOrderList() {
		return orderList;
	}
	public void setOrderList(List<Order> orderList) {
		this.orderList = orderList;
	}
	private CommonService commonService;
	private PackageCompose packageCompose;
 
	private boolean clearCarFlag;
	public String getPriceAndColor() {
		return priceAndColor;
	}
	public long getProductHiddenSession() {
		return productHiddenSession;
	}
	public void setProductHiddenSession(long productHiddenSession) {
		this.productHiddenSession = productHiddenSession;
	}
	public void setPriceAndColor(String priceAndColor) {
		this.priceAndColor = priceAndColor;
	}
	public long getSeqTime() {
		return seqTime;
	}
	public void setFittingService(FittingService fittingService) {
		this.fittingService = fittingService;
	}
	public void setSeqTime(long seqTime) {
		this.seqTime = seqTime;
	}
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	public PackageCompose getPackageCompose() {
		return packageCompose;
	}
	public boolean isClearCarFlag() {
		return clearCarFlag;
	}
	public void setClearCarFlag(boolean clearCarFlag) {
		this.clearCarFlag = clearCarFlag;
	}
	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}
	public void setPackageCompose(PackageCompose packageCompose) {
		this.packageCompose = packageCompose;
	}
	public int getGoodstype() {
		return goodstype;
	}
	public void setGoodstype(int goodstype) {
		this.goodstype = goodstype;
	}
	
 
	public String order2() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
		Object sessionObj = request.getSession().getAttribute("windfoneSessioin");
		if ( sessionObj !=null ) {
			orderList =  (List)sessionObj;
		}
		return SUCCESS;
	}
	public String getPrice() {
		return price;
	}
	public int getProductId() {
		return productId;
	}
 
	public void setPrice(String price) {
		this.price = price;
	}
	public void setProductId(int productId) {
		this.productId = productId;
	}
 
	public String order1() throws Exception {
		return SUCCESS;
	}
	public String orderShow() throws Exception {
		return SUCCESS;
	}
	
	public String clearCar() throws Exception {
		clearCarFlag = false;
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
		Object sessionObj = request.getSession().getAttribute("windfoneSessioin");
		if ( sessionObj !=null ) {
			List myOrderList =  (List)sessionObj;
			myOrderList.clear();
			request.getSession().removeAttribute("windfoneSessioin");
			clearCarFlag = true;
		} else {
			clearCarFlag = true;
		}
		return SUCCESS;
	}
	  
	public String changeCount() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
		Object sessionObj = request.getSession().getAttribute("windfoneSessioin");
		if ( sessionObj !=null ) {
			List myOrderList =  (List)sessionObj;
			for(int i=0;i<myOrderList.size();i++){
				if ( ((Order)myOrderList.get(i)).getTimeSeq() == seqTime ){
					Order order = (Order)myOrderList.get(i);
					order.setOrderNumber(orderNumber);
				}
			}
		}  
		return SUCCESS;
	}
	
	public String delGoods() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
		Object sessionObj = request.getSession().getAttribute("windfoneSessioin");
		if ( sessionObj !=null ) {
			List myOrderList =  (List)sessionObj;
			for(int i=0;i<myOrderList.size();i++){
				if ( ((Order)myOrderList.get(i)).getTimeSeq() == seqTime ){
					myOrderList.remove(i);
				}
			}
		}  
		return SUCCESS;
	}
	public String fetchNewssion() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
		Object token = request.getSession().getAttribute("productSession");
		if ( token!= null){
			request.getSession().removeAttribute("productSession");
		}
		newProductHiddenSession = (new Date()).getTime();
		request.getSession().setAttribute("productSession",newProductHiddenSession);
		return SUCCESS;
	}
	public String myShoppingCar() throws Exception {
		
		if ( productId !=-1 ){
			Order order = new Order();
			order.setTimeSeq( (new Date()).getTime());
			order.setGoodstype(goodstype);
			order.setProductId(productId);
			if ( priceAndColor!=null && !priceAndColor.equals("")){
				int index = priceAndColor.indexOf("_");
				price = priceAndColor.substring(0,index);
				String packageIdStr = priceAndColor.substring(index+1,priceAndColor.length());
				packageCompose = commonService.getPackageByKey(Integer.parseInt(packageIdStr));
				if (packageCompose != null)
					order.setColorName(packageCompose.getCompose());
			}
			priceAndColor = "";
			if ( goodstype==0){
				Product mobile = productService.getProductById(productId).get(0);
				order.setImagepath(mobile.getImagepath());
				order.setPrice(Double.parseDouble(price));
				order.setStandard(mobile.getStandard());
				order.setName(mobile.getName() );
			} else if ( goodstype == 1){
				Fitting mobile = fittingService.getFittingById(productId).get(0);
				order.setImagepath(mobile.getImagepath());
				order.setPrice(Double.parseDouble(price));
				order.setStandard(mobile.getStandar());
				order.setName(mobile.getTypeName() );
			}
			ActionContext ctx = ActionContext.getContext();
			HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
			Object sessionObj = request.getSession().getAttribute("windfoneSessioin");
			Object token = request.getSession().getAttribute("productSession");
			if ( token !=null ){
				long tokenSession =  Long.parseLong(token.toString());
				if ( tokenSession == productHiddenSession) {
					if ( sessionObj !=null ) {
						orderList =  (List)sessionObj;
						orderList.add(order);
						request.getSession().setAttribute("windfoneSessioin",orderList);
					} else {
						orderList = new ArrayList<Order>();
						orderList.add(order);
						request.getSession().setAttribute("windfoneSessioin",orderList);
					}
					request.getSession().removeAttribute("productSession" );
				}
			}
		}
		return SUCCESS;
	}
}
